package ctrhost

import (
	"github.com/astaxie/beego/logs"
	"fmt"
	"cloud_worm/util/umodels/mysqldb"
	"cloud_worm/util/umodels/mysqldb/dbmanager"
	"cloud_worm/util/uhttp"
	"cloud_worm/wormweb/controllers/ctrutil"
	"cloud_worm/wormweb/controllers/ctrconfig"
)

type AgentController struct {
	ctrconfig.LoginCtr
}

// https://{domain}:{port}/host/connect/all?page_count=&page=&status=&worm_id=&start_time=&end_time=
func (a *AgentController) List() {
	a.Manager()

	offset, limit := a.DealPage()
	status, err := a.GetInt("status", mysqldb.StatusAll)

	wormId := a.GetString("worm_id", mysqldb.SelectStrAll)
	ip := a.GetString("ip", mysqldb.SelectStrAll)
	serverAddr := a.GetString("server_addr", mysqldb.SelectStrAll)
	platform := a.GetString("platform", mysqldb.SelectStrAll)

	version, err := a.GetInt("version", mysqldb.SelectIntAll)

	startTime, err := a.GetInt("start_time", mysqldb.SelectIntAll)				// 开始时间条件
	endTime, err := a.GetInt("end_time", mysqldb.SelectIntAll)					// 结束时间条件
	
	// count读取
	worm := &dbmanager.WormId{}
	count, err := worm.Count(status, version, wormId, ip, platform, serverAddr, startTime, endTime, ctrutil.RunMode)
	if err != nil {
		logs.Error(fmt.Sprintf("error in get count: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.WormConnectionInfo{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, err.Error(), data)
		a.ServeJSON()
		return
	}
	if count == 0 {
		data := map[string]interface{}{"total": 0, "data": []dbmanager.WormConnectionInfo{}}
		a.Data["json"] = uhttp.CreateOkResponse(data)
		a.ServeJSON()
		return
	}
	
	// select: GetConnectionInfo
	agents, err := worm.GetConnectionInfo(offset, limit, status, version, wormId, ip, platform, serverAddr,
		startTime, endTime, ctrutil.RunMode)
	if err != nil {
		logs.Error(fmt.Sprintf("select all online failed: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.WormConnectionInfo{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select failed.", data)
		a.ServeJSON()
		return
	}

	data := map[string]interface{}{"total": count, "data": agents}
	a.Data["json"] = uhttp.CreateOkResponse(data)
	a.ServeJSON()
	return
}

// AgentController.ServerList 获取server的列表
func (a *AgentController) AccessSrvList() {
	a.Manager()

	worm := &dbmanager.WormId{}
	servers, err := worm.SelectAccessSrvList(mysqldb.StatusValid)
	if err != nil {
		logs.Error(fmt.Sprintf("select access server list error: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []string{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select access server list error", data)
		a.ServeJSON()
		return
	}

	var l []string
	for _, item := range servers {
		if len(item.ServerAddr) <= 0 {
			continue
		}
		l = append(l, item.ServerAddr)
	}
	data := map[string]interface{}{"total": len(l), "data": l}
	a.Data["json"] = uhttp.CreateOkResponse(data)
	a.ServeJSON()
	return
}

// AgentController.VersionList 获取version的list
func (a *AgentController) VersionList() {
	a.Manager()

	worm := &dbmanager.WormId{}
	versions, err := worm.SelectVersionList(mysqldb.StatusAll)
	if err != nil {
		logs.Error(fmt.Sprintf("select version list error: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []int{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select version list error", data)
		a.ServeJSON()
		return
	}

	var l []int
	for _, item := range versions {
		if item.Version <= 0 {
			continue
		}
		l = append(l, item.Version)
	}
	data := map[string]interface{}{"total": len(l), "data": l}
	a.Data["json"] = uhttp.CreateOkResponse(data)
	a.ServeJSON()
	return
}

// AgentController.PlatformList 获取管理机器的平台列表
func (a *AgentController) PlatformList() {
	a.Manager()

	worm := &dbmanager.WormId{}
	platforms, err := worm.SelectPlatformList(mysqldb.StatusAll)
	if err != nil {
		logs.Error(fmt.Sprintf("select platform list error: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []string{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select platform list error", data)
		a.ServeJSON()
		return
	}

	var l []string
	for _, item := range platforms {
		if len(item.Platform) <= 0 {
			continue
		}
		l = append(l, item.Platform)
	}
	data := map[string]interface{}{"total": len(l), "data": l}
	a.Data["json"] = uhttp.CreateOkResponse(data)
	a.ServeJSON()
	return
}